/*
#include "ui_timeline.h"
#include "ui_widget.h"
#include "ui_manager.h"

#include "script/scripthelper.h"

#include "util/timer.h"		    // Needs to come in early
#include "util/mmgr.h"		    // Needs to come in early
#include "util/common_macros.h"		// macros
#include "util/log.h"		    // logging

#include <algorithm>
#include <cassert>


using namespace script;
using namespace ui;

UITimeLine::UITimeLine(UIWidget* pOwner) : 
	m_MinTime(0),
	m_MaxTime(0),
	m_Time(0),
	m_CurrentKey(0),
	m_pOwner(pOwner)
{
	assert(m_pOwner && "Invalid owner!");
	memset(&m_CurrentKeyframe, 0, sizeof(m_CurrentKeyframe));
}

UITimeLine::~UITimeLine()
{
	int Count = (int)m_Keyframes.size();
	for (int i=0; i<Count; i++)
	{
		Keyframe*	pKey = m_Keyframes[i];
		UTIL_SAFE_DELETE(pKey);
	}
}

math::Vec4	DecodeColor(COLOR	Col)
{
//	((D3DCOLOR)((((a)&0xff)<<24)|(((r)&0xff)<<16)|(((g)&0xff)<<8)|((b)&0xff)))

	const float ByteToFloat = 1.0f / 255.0f;
	return math::Vec4(
		((Col >> 16) & 0xFF) * ByteToFloat,
		((Col >> 8 ) & 0xFF) * ByteToFloat,
		((Col      ) & 0xFF) * ByteToFloat,
		((Col >> 24) & 0xFF)* ByteToFloat);
}

bool UITimeLine::Load(LuaPlus::LuaObject& _LObj)
{
	math::Vec4	ParentColor = DecodeColor(m_pOwner->GetColor());
	math::Vec2	ParentUV1, ParentUV2;
	m_pOwner->GetUV(ParentUV1, ParentUV2);

	if (_LObj.IsTable())
	{
		int	Count = _LObj.GetCount();
		for (int i=0; i<Count; i++)
		{
			LuaPlus::LuaObject& Key = _LObj.GetByIndex(i+1);
			if (Key.IsTable())
			{
				math::Vec2	Pos			= ScriptHelper::GetTableVec2(Key, "Pos", m_pOwner->GetPosition());
				math::Vec2	Size		= ScriptHelper::GetTableVec2(Key, "Size", m_pOwner->GetSize());
				math::Vec2	UV1			= ScriptHelper::GetTableVec2(Key, "UV1", ParentUV1);
				math::Vec2	UV2			= ScriptHelper::GetTableVec2(Key, "UV2", ParentUV2);
				math::Vec4	Color		= ScriptHelper::GetTableVec4(Key, "Color", ParentColor);
				float		Time		= ScriptHelper::GetTableFloat(Key, "Time", 0);
				float		Rotation	= ScriptHelper::GetTableFloat(Key, "Rotation", m_pOwner->GetRotation());
				float		Scale		= ScriptHelper::GetTableFloat(Key, "Scale", m_pOwner->GetScale());
				std::string Script		= ScriptHelper::GetTableString(Key, "Script", "");
				AddKeyframe(Time, Pos, Size, Color, Rotation, Scale, UV1, UV2, Script);
			}
		}
	}
	Finalize();
	return true;
}

void UITimeLine::AddKeyframe(float t, const math::Vec2& Pos, const math::Vec2& Size, 
							 const math::Vec4& Color, float Rot, float Scale, 
							 const math::Vec2& UV1, const math::Vec2& UV2, 
							 const std::string& Script)
{
	Keyframe*	pKey = new Keyframe();
	pKey->Time = t;
	pKey->Pos  = Pos;
	pKey->Size = Size;
	pKey->Color = Color;
	pKey->Rotation = Rot;
	pKey->Scale = Scale;
	pKey->UV[0] = UV1;
	pKey->UV[1] = UV2;
	pKey->Script = Script;
	m_Keyframes.push_back(pKey);
}


static bool SortFunc( UITimeLine::Keyframe* pA, UITimeLine::Keyframe* pB)
{
   return pA->Time < pB->Time;
}


void UITimeLine::Finalize()
{
	if (m_Keyframes.size() == 0)
		return;	// cannot sort empty list
	std::sort(m_Keyframes.begin(), m_Keyframes.end(), SortFunc);
	
	m_MinTime = m_Keyframes[0]->Time;
	m_MaxTime = m_Keyframes[m_Keyframes.size() - 1]->Time;
	SetTime(m_MinTime);
}

static float LerpFloat(float from, float to, float t)
{
	if (t < 0.0f) t = 0.0f;
	if (t > 1.0f) t = 1.0f;

	float	Delta = to - from;
	return from + (Delta*t);
}

void	UITimeLine::ExecuteScript(Keyframe*	pKey)
{
	if (!pKey->Script.empty())
	{
		// execute script
		UIManager::Instance().ExecuteScript(pKey->Script);
	}
}

void UITimeLine::Tick(float dt)
{
	int LastKey = m_CurrentKey;
	SetTime(m_Time + dt);
	// did we pass over one or more keyframes?
	if (LastKey != m_CurrentKey)
	{
		if (LastKey < m_CurrentKey)
		{
			// execute all of them
			for (int i=LastKey+1; i<=m_CurrentKey; i++)
				ExecuteScript(m_Keyframes[i]);
		} else
		{
			// if we went past the last element in the array
			for (unsigned int i=LastKey+1; i<m_Keyframes.size(); i++)
				ExecuteScript(m_Keyframes[i]);
			for (int i=0; i<=m_CurrentKey; i++)
				ExecuteScript(m_Keyframes[i]);
		}
	}
}

void UITimeLine::BuildKeyframe()
{
	// update keyframe
	Keyframe*	pCurrent = m_Keyframes[m_CurrentKey];
	Keyframe*	pNext = pCurrent;		// if we don't have a next, use the same

	// more spaces in the vector?
	if (m_CurrentKey < (int)m_Keyframes.size() - 1)
	{
		pNext = m_Keyframes[m_CurrentKey+1];
	} else
	{
		pNext = m_Keyframes[0];
	}
	
	// interpolation factor
	float		Interpolation = (m_Time - pCurrent->Time) / (pNext->Time - pCurrent->Time);

	m_CurrentKeyframe.Color		= LerpVec4(pCurrent->Color, pNext->Color, Interpolation);
	m_CurrentKeyframe.Pos		= LerpVec2(pCurrent->Pos, pNext->Pos, Interpolation);
	m_CurrentKeyframe.Size		= LerpVec2(pCurrent->Size, pNext->Size, Interpolation);
	m_CurrentKeyframe.UV[0]		= LerpVec2(pCurrent->UV[0], pNext->UV[0], Interpolation);
	m_CurrentKeyframe.UV[1]		= LerpVec2(pCurrent->UV[1], pNext->UV[1], Interpolation);
	m_CurrentKeyframe.Rotation	= LerpFloat(pCurrent->Rotation, pNext->Rotation, Interpolation);
	m_CurrentKeyframe.Scale		= LerpFloat(pCurrent->Scale, pNext->Scale, Interpolation);
}

float UITimeLine::GetTime()
{
	return m_Time;
}

void UITimeLine::SetTime(float t)
{
	// wraps
	if ((t > m_MaxTime) || (t < m_MinTime))
	{
		t = m_MinTime;
		m_CurrentKey = 0;
	}
	m_Time = t;
	
	// more spaces in the vector?
	if (m_CurrentKey < (int)m_Keyframes.size() - 1)
	{
		if (m_Time >  m_Keyframes[m_CurrentKey+1]->Time)
			m_CurrentKey++;
	}
	BuildKeyframe();
}

const math::Vec2&	UITimeLine::GetPos()			
{
	return m_CurrentKeyframe.Pos;
}

const math::Vec2&	UITimeLine::GetSize()			
{
	return m_CurrentKeyframe.Size;
}

const math::Vec4&	UITimeLine::GetColor()
{
	return m_CurrentKeyframe.Color;
}

float UITimeLine::GetRotation()
{
	return m_CurrentKeyframe.Rotation;
}

float UITimeLine::GetScale()
{
	return m_CurrentKeyframe.Scale;
}

const math::Vec2& UITimeLine::GetUV1()
{
	return m_CurrentKeyframe.UV[0];
}

const math::Vec2& UITimeLine::GetUV2()
{
	return m_CurrentKeyframe.UV[1];
}
*/
